app.register.controller('loginCtrl',['$scope','$location', '$http','$cookies', function ($scope,$location,$http,$cookies) {
    // 数据
    $scope.user = {
        tellTop : '86-'
    };

    //登录
    $scope.submitLogin = function () {
        //验证
        if(!formVerity()) return;
        console.log('666');
        //提交登录验证
        $http({
            method:'POST',
            url:$config.host+'/api/users/getSalt',
            params:{
                uName : $scope.user.tellTop+$scope.user.name
            }
        }).success(function (json) {
            if(json.nFlag==1){
                $http({
                    method:'POST',
                    url:$config.host+'/api/users/Login',
                    params:{
                        strPassword : md5(json.data.strSalt+$scope.user.pwd),
                        nUserId : json.data.nUserId,
                        t : new Date().getTime(),
                        strTokenId : json.data.strTokenId,
                        strHash : md5(json.data.strTokenId+json.data.nUserId)

                    }
                }).success(function (data) {
                    // console.log(JSON.stringify(json));
                    if(data.nFlag==1){
                        $cookies.strTokenId = data.data.strTokenId;
                        $cookies.nUserId = data.data.nUserId;
                        if($cookies.nEventId){
                            $location.path('/detail/'+$cookies.nEventId);
                        }else{
                            $location.path('/home');
                        }
                    }else{
                        $scope.popShow(data.strMessage);
                    }
                })
            }else {
                $scope.popShow(json.strMessage);
            }
        });
    }

    //表单验证
    function formVerity() {
        if(!$scope.user.name){
            $scope.popShow('手机号不能为空！');
            return false;
        }
        if(!$config.rex.gattel.test($scope.user.name)){
            $scope.popShow('手机号格式不正确！');
            return false;
        }
        if(!$scope.user.pwd){
            $scope.popShow('密码不能为空！');
            return false;
        }
        return true;
    }

}]);